#include <iostream>
#include <string>
using namespace std;

#include "bufDBClient.h"

bufDBClient::bufDBClient()
{
	readLine= "unInitialized";
	fieldCnt = 0;
	msgStart = 0;

}

////////////////////////////////////////////////////////////////////////////////
// split()
//
// Description: This function will split the data from the string
//
// Inputs: const string& s, char c, vector<string> &v
// 
//
// Outputs: none
//
// Return: none
/////////////////////////////////////////////////////////////////////////////////
void bufDBClient::split(const string& s, char c, vector<string> &v) 
{
    string::size_type i = 0;
    string::size_type j = s.find(c);

    while (j != string::npos) {
        v.push_back(s.substr(i, j-i));
        i = ++j;
        j = s.find(c, j);
        if (j == string::npos)
            v.push_back(s.substr(i, s.length( )));
    }
}


////////////////////////////////////////////////////////////////////////////////
// messagesInFile()
//
// Description: This function will figure out how many messages are in the file
//
// Inputs: string filename, int *msgSize
// 
//
// Outputs: none
//
// Return: totalMsgCnt
/////////////////////////////////////////////////////////////////////////////////
int bufDBClient::messagesInFile(string filename, int *msgSize)
{
    ifstream f(filename.c_str(), ifstream::in);
    //First, split file into seperate messages (tables in DB)
    //and determine number of fields of each.
    int totalMsgCnt = 0;
    int i = 0;
    int j = 0;
	while(getline(f, readLine))
    {
		inData.push_back(readLine);
		if(readLine == "==")
		{
			msgSize[totalMsgCnt] = i-j;
			j=i+1;
			cout<<"Message "<<totalMsgCnt<<" is of size "<<msgSize[totalMsgCnt]<<endl;
			++totalMsgCnt;					
		}			
		++i;
	}
	
	return totalMsgCnt;
}


////////////////////////////////////////////////////////////////////////////////
// createFieldTokens()
//
// Description: This function will create the field tokens
//
// Inputs: int fieldsInMsg
// 
//
// Outputs: none
//
// Return: none
/////////////////////////////////////////////////////////////////////////////////
void bufDBClient::createFieldTokens(int fieldsInMsg)
{
   while (fieldCnt<msgStart+fieldsInMsg)
    {
        split(inData[fieldCnt], ',', tokens);
		fieldCnt++;
    }
    
    //This last increment moves the field counter past the
    //message delemiter, and tells the function what line
    //to start on next.
    fieldCnt++;
    msgStart = fieldCnt;
}


////////////////////////////////////////////////////////////////////////////////
// getTokenSize()
//
// Description: This function will get the token size
//
// Inputs: none
// 
//
// Outputs: none
//
// Return: tokens.size()
/////////////////////////////////////////////////////////////////////////////////
int bufDBClient::getTokenSize()
{
	return tokens.size();
}



////////////////////////////////////////////////////////////////////////////////
// setTokensInProto()
//
// Description: This function will put the tokens into the proto file
//
// Inputs: none
// 
//
// Outputs: none
//
// Return: none
/////////////////////////////////////////////////////////////////////////////////
void bufDBClient::setTokensInProto(int fieldNum)
{
	bufDBClient::setNestedSingleFieldFromString(tokens[fieldNum]);
}



////////////////////////////////////////////////////////////////////////////////
// getTokensFromProto()
//
// Description: This function will take the tokens from the proto file
//
// Inputs: none
// 
//
// Outputs: none
//
// Return: none
/////////////////////////////////////////////////////////////////////////////////
void bufDBClient::getTokensFromProto()
{

	//First Determine how many messages are in the file
	//The deque inData contains the data read from the file.
	//The array msgSize contains the size of each of the messages
	//ie: The number of tokens in each
	//int totalInputMsgs = sever.messagesInFile(filename, msgSize);
	bufDBClient::setNewMessage(msg);	
	int totalProtoMsgs = bufDBClient::findRepeatedFields();//default value is 2
	
	int totalNestedTypes = bufDBClient::findNestedTypes();//default is 2 tell us where the player or team is 
	
	
	/**/
					
	//Iterate through each message and store the fields
	//into seperate tokens.
	for(int msgCnt = 0; msgCnt < totalProtoMsgs; msgCnt++)
	{

		//Set your indexes to point to the correct field
		//Repeated field gives you the data
		//Nested field gives you the type of data
		bufDBClient::setRepeatedMsgPtr(msgCnt);
		bufDBClient::setNestedTypePtr(msgCnt);
		
		//Once you are pointing at the correct Nested Type
		//you must determine the type of each token in the Nested Type
		//This is equivalent the type of each comma seperated value
		//on a line of the input file.
		int totalNestedTokenTypes = bufDBClient::findNestedTokenTypes();	//default value should be 2	
			
		for(int entryCnt = 0; entryCnt<bufDBClient::getEntryCnt(); entryCnt++)
		{
			bufDBClient::setMessageEntryPtr(entryCnt);
			
			for(int tokenTypeCnt=0; tokenTypeCnt<totalNestedTokenTypes;tokenTypeCnt++)
			{
			
				bufDBClient::setNestedTokenTypePtr(tokenTypeCnt);//setting pointer of name and batting_avg
				//bufDBClient::getDataTypeAndStore();//getting data from entry specified by entryCnt
			}
					
		}
	}
}	
	
